package de.wowrealmstatus.worker

import de.wowrealmstatus.dc.WoWRealm 
import de.wowrealmstatus.types.Language;
import de.wowrealmstatus.types.NYIException 
import de.wowrealmstatus.types.Population;
import de.wowrealmstatus.types.RealmType;
import de.wowrealmstatus.types.Region 

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;



class WoWRealmStatus {
	def private final static US_URL = "http://www.worldofwarcraft.com/realmstatus/status.xml"
	def private final static EU_URL = "http://www.wow-europe.com/realmstatus/index.html?locale=en_us"
	
	List<WoWRealm> realms = new ArrayList<WoWRealm>()
	
	def private WoWRealmStatus(String xml, Region region) {
		switch (region) {
			case Region.US:
			parseUSXML xml
			break;
			case Region.Europe:
			parseEUHTML xml
			break;
			default:
			break;
		}
	}
	
	def parseUSXML (String xml) {
		realms.clear()
		def page = new XmlSlurper().parseText(xml)
		
		def allRealms = page.depthFirst().grep { it.name() == "r" }
		
		allRealms.each {
			def name = it.@n.toString()
			def realm = WoWRealm.findByRegionAndName (Region.US, name)
			if (realm == null) {
				realm = new WoWRealm()
				realm.name = name
				realm.region = Region.US
			}
			realm.status = (boolean)(it.@s == "1")
			realm.type = RealmType.getRealmType(it.@t.toString())
			realm.population = Population.getPopulation(it.@l.toString())
			realm.language = Language.English
			
			realms.add realm
		}
	}
	
	def parseEUHTML (String html) {
		realms.clear()
		def parser = new org.cyberneko.html.parsers.SAXParser()
		parser.setFeature('http://xml.org/sax/features/namespaces', false)
		def page = new XmlParser(parser).parse(html)
		def data = page.depthFirst().TR.grep { 
			it != null && 
			it.size() > 0 &&
			it.TD.'@class' != null && 
//			(println (it.TD.'@class')) &&
			it.TD.'@class'.get(0).toString().startsWith("serverStatus") 
		} 
		println data.size()
		data.each { 
			def name = it.TD.A.@name.get(0).toString()
			def realm = WoWRealm.findByRegionAndName (Region.Europe, name)
			if (realm == null) {
				realm = new WoWRealm()
				realm.name = name
				realm.region = Region.Europe
			}
			realm.status = it.TD.IMG.@src.get(0).contains("uparrow.gif")
			realm.type = RealmType.getRealmType (it.TD.SMALL.text().toString())
			realm.population = Population.getPopulation(it.TD.SPAN.B.FONT.text().toString())
			realm.language = Language.Unknown
			realms.add realm
		}
	}
	
	def static List<WoWRealm> getStatusList (Region region) {
		def url
		switch (region) {
			case Region.US:
				url = wget(US_URL)
				break
			case Region.Europe:
				url = EU_URL
				break
			default:
				throw new NYIException()
				break
		}
		return new WoWRealmStatus(url, region).realms
	}
	
	
	
	def static String wget (String urlString) {
		String result = ""
		URL url = new URL(urlString);
		URLConnection uc = url .openConnection()
		BufferedReader br = new BufferedReader(
				new InputStreamReader(
				uc.getInputStream()))
		String inputLine=null
		
		while ((inputLine = br.readLine()) != null)
			result += inputLine
		br.close()
		return result
	}
	
	def static Map<String, WoWRealm> getStatusMap (Region region) {
		def statusList = getStatusList (region)
		def result = new HashMap<String, WoWRealm>()
		
		for (realm in statusList) {
			result.put (realm.name, realm)
		}
		return result
	}
}
